.TH std::ranges::view_interface::operatorbool 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::view_interface::operatorbool \- std::ranges::view_interface::operatorbool

.SH Synopsis
   constexpr explicit operator bool() requires /* see below */;       \fB(1)\fP \fI(since C++20)\fP
   constexpr explicit operator bool() const requires /* see below */; \fB(2)\fP \fI(since C++20)\fP

   The default implementation of operator bool member function checks whether the view
   is non-empty. It makes the derived type contextually convertible to bool.

   1) Let derived be static_cast<D&>(*this). The expression in the requires-clause is
   equal to requires { ranges::empty(derived); }, and the function body is equivalent
   to return !ranges::empty(derived);.
   2) Same as \fB(1)\fP, except that derived is static_cast<const D&>(*this).

.SH Parameters

   \fI(none)\fP

.SH Return value

   false if the value of the derived type is empty (determined by std::ranges::empty),
   true otherwise.

.SH Notes

   In C++20, no type derived from std::ranges::view_interface in the standard library
   provides their own operator bool. Almost all of these types use the default
   implementation.

   A notable exception is std::ranges::basic_istream_view. For its iterator type never
   satisfies forward_iterator, the view cannot use the inherited operator bool.

.SH Example


// Run this code

 #include <array>
 #include <iostream>
 #include <ranges>

 int main()
 {
     const std::array ints {0, 1, 2, 3, 4};
     auto odds = ints | std::views::filter([](int i) { return 0 != i % 2; });
     auto negs = ints | std::views::filter([](int i) { return i < 0; });
     std::cout << std::boolalpha
               << "Has odd numbers: " << (!!odds) << ' ' << '\\n'
               << "Has negative numbers: " << (!!negs) << ' ' << '\\n';
 }

.SH Output:

 Has odd numbers: true
 Has negative numbers: false

.SH See also

   ranges::empty checks whether a range is empty
   (C++20)       (customization point object)
   empty         returns whether the derived view is empty. Provided if it satisfies
   (C++20)       sized_range or forward_range.
                 \fI(public member function)\fP
   empty         checks whether the container is empty
   \fI(C++17)\fP       \fI(function template)\fP
